package com.ebt.m.view.timeline;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.activity.WebBaseActivity;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.data.rxModel.apibean.OpportunityDataResult;
import com.ebt.m.utils.TalkingDataUtil;
import com.ebt.m.utils.TimeUtils;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SectionAlertView extends IView {


    @BindView(R.id.message_img)
    ImageView messageImg;
    @BindView(R.id.message_title)
    TextView messageTitle;
    @BindView(R.id.message_time)
    TextView messageTime;
    @BindView(R.id.message_content)
    TextView messageContent;
    @BindView(R.id.item_root)
    RelativeLayout itemRoot;
    private OpportunityDataResult.AlertMessageEntity caseObj;
    private OpportunityDataResult opportunityDataResult;

    public SectionAlertView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_message_item, this);
        ButterKnife.bind(this);
    }

    public SectionAlertView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SectionAlertView(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... obj) {
        try {
            opportunityDataResult = (OpportunityDataResult) obj[0];
            Gson gson = new Gson();
            caseObj = gson.fromJson(opportunityDataResult.getCaseObj(), OpportunityDataResult.AlertMessageEntity.class);
            int status = opportunityDataResult.getStatus();
            setReadStatus(status);
            messageTitle.setText(caseObj.getTitle());
            messageContent.setText(caseObj.getMessage());
            messageTime.setText(TimeUtils.getMessageDate(opportunityDataResult.getCreateTime()));
        } catch (Exception e) {
            Logger.e(e);
        }

    }

    @OnClick({R.id.item_root})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_root:
                setReadStatus(1);
                if (opportunityDataResult != null && opportunityDataResult.getStatus() == 0)
                    mOnclickListner.onClick(view, opportunityDataResult.getId());
                TalkingDataUtil.onEvent(TalkingDataUtil.TYPE_BO_READ);
                gotoTrace();
                break;
        }
    }

    public void gotoTrace() {
        if (caseObj != null && !TextUtils.isEmpty(caseObj.getUrl())) {
            TalkingDataUtil.onEvent(TalkingDataUtil.TYPE_BO_TRACE);
            Intent webIntent = new Intent(getContext(), WebBaseActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, getResources().getString(R.string.str_alert_birthday));
            bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, WebBaseActivity.FRAGMENT_4NEW_BASE);
            bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, false);
            bundle.putString(WebBaseActivity.WEB_TYPE_LINK, caseObj.getUrl());
            webIntent.putExtras(bundle);
            getContext().startActivity(webIntent);
        }
    }

    /**
     * @param status 0  未读   1 已读
     */
    private void setReadStatus(int status) {
        int subType = caseObj.getSubtype();
        if (status == 0) {
            if (subType == 2) {
                messageImg.setBackgroundResource(R.drawable.message_birtyday_unchecked);
            } else if (subType == 1) {
                messageImg.setBackgroundResource(R.drawable.message_licence_unchecked);
            }
            messageTitle.setTextColor(Color.parseColor("#333333"));
            messageContent.setTextColor(Color.parseColor("#333333"));
        } else {
            messageTitle.setTextColor(Color.parseColor("#BBBBBB"));
            messageContent.setTextColor(Color.parseColor("#BBBBBB"));
            if (subType == 2) {
                messageImg.setBackgroundResource(R.drawable.message_birtyday_checked);
            } else if (subType == 1) {
                messageImg.setBackgroundResource(R.drawable.message_licence_checked);
            }
        }
    }

}
